﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class ContactOffice
    {
         #region Common
        public eContactOffice   Data;

        public ContactOffice()
        {
            Data = new eContactOffice();
		}

        ~ContactOffice()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cContactOffice objDAL = new cContactOffice();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cContactOffice objDAL = new cContactOffice();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cContactOffice objInfor = new cContactOffice(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.ContactOfficeID ;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cContactOffice objInfor = new cContactOffice();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

      

        private void MappingData(cContactOffice objInfor)
        {


            try { objInfor.Name1 = Data.Name1; }
            catch { objInfor.Name1 = ""; }

            try { objInfor.Name2 = Data.Name2; }
            catch { objInfor.Name2 = ""; }

            try { objInfor.Name3 = Data.Name3; }
            catch { objInfor.Name3 = ""; }

            try { objInfor.Address1 = Data.Address1; }
            catch { objInfor.Address1 = ""; }

            try { objInfor.Address2 = Data.Address2; }
            catch { objInfor.Address2 = ""; }

            try { objInfor.Address3 = Data.Address3; }
            catch { objInfor.Address3 = ""; }

            try { objInfor.Tel1 = Data.Tel1; }
            catch { objInfor.Tel1 = ""; }

            try { objInfor.Tel2 = Data.Tel2; }
            catch
            {
                objInfor.Tel2 = "";
            }
            try { objInfor.Tel3 = Data.Tel3; }
            catch { objInfor.Tel3 = ""; }

            try { objInfor.Fax1 = Data.Fax1; }
            catch { objInfor.Fax1 = ""; }

            try { objInfor.Fax2 = Data.Fax2; }
            catch { objInfor.Fax2 = ""; }

            try { objInfor.Fax3 = Data.Fax3; }
            catch { objInfor.Fax3 = ""; }


            try { objInfor.Email = Data.Email; }
            catch { objInfor.Email = ""; }

            try { objInfor.Website = Data.Website; }
            catch { objInfor.Website = ""; }
          
        }

        private void ConvertData(cContactOffice objInfor)
        {
            Data.Name1 = objInfor.Name1;
            Data.Name2 = objInfor.Name2;
            Data.Name3 = objInfor.Name3;
            Data.Address1  =objInfor.Address1   ;
            Data.Address2 = objInfor.Address2;
            Data.Address3 = objInfor.Address3;
            Data.Tel1 = objInfor.Tel1;
            Data.Tel2 = objInfor.Tel2;
            Data.Tel3 = objInfor.Tel3;
            Data.Fax1 = objInfor.Fax1;
            Data.Fax2 = objInfor.Fax2;
            Data.Fax3 = objInfor.Fax3;
            Data.Email = objInfor.Email;
            Data.Website = objInfor.Website ;
            
            
        }
        #endregion

        public DataTable Search()
        {
            DataTable tbResult = new DataTable();
            cContactOffice objDAL = new cContactOffice();
            tbResult = objDAL.Search();
            return tbResult;
        } 
    }
}
